﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Windows.Forms;
using Microsoft.Win32;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Drawing;
using AForge.Video.DirectShow;
using System.Drawing.Imaging;
using AForge.Video;
using AForge.Controls;
using AForge.Imaging.Filters;
using System.Threading;

namespace VCMW
{
    public partial class FrmMain : Form
    {
        Logs log = new Logs();
        //是否调试模式
        bool debug = false;
        Debug debugFrm = new Debug();
        int x = 0; int y = 0; int width = 0; int height = 0;
        private FilterInfoCollection videoDevices;//调取摄像头
        //获取项目的根目录
        String path = Directory.GetCurrentDirectory();
        //private PCCamera pcc;
        public FrmMain()
        {
            InitializeComponent();
            //设置视频宽高为控件的宽高
            //pcc = new PCCamera(this.videoSourcePlayer1.Handle, 0, 0, this.videoSourcePlayer1.Width, this.videoSourcePlayer1.Height);//panelCamera是一个panel控件
            this.DoubleBuffered = true;//设置本窗体
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0014) // 禁掉清除背景消息
                return;
            base.WndProc(ref m);
        }
        string temppath = "";
        //加载
        private void FrmMain_Load(object sender, EventArgs e)
        {
            try
            {
                System.Windows.Forms.PictureBox[] picture = new System.Windows.Forms.PictureBox[40];
                picture[0] = jk01;
                //设置仪表盘色彩
                this.aq_qingxu.Range_Idx = 0;
                this.aq_qingxu.RangeColor= Color.FromArgb(68, 102, 244);
                this.aq_qingxu.Range_Idx = 1;
                this.aq_qingxu.RangeColor = Color.FromArgb(142, 68, 191);
                this.aq_qingxu.Range_Idx = 2;
                this.aq_qingxu.RangeColor = Color.FromArgb(255, 16, 110);
                string[] path_arr = path.Split('\\');
                for (int i = 0; i < path_arr.Length; i++)
                {
                    if (path_arr[i].Trim().ToString() == "VCMW")
                    {
                        temppath += path_arr[i];
                        break;
                    }
                    else
                    {
                        temppath += path_arr[i] + "\\";
                    }
                }

                this.BackColor = Color.FromArgb(9, 9, 24);
                this.aq_qingxu.BackColor = Color.FromArgb(9, 9, 24);
                //this.MaximizeBox = false;
                //this.MinimizeBox = false;
                //设置窗口边框
                //this.ControlBox = false;
                //this.FormBorderStyle = FormBorderStyle.None;
                
                this.videoSourcePlayer1.Width = this.panel_left.Width;
                this.videoSourcePlayer1.Height = this.panel_left.Height;
                //自动打开摄像头

                // 枚举所有视频输入设备
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                if (videoDevices.Count == 0)
                {
                    this.labtitle.Text = "未检测到摄像头！";
                    return;
                }
                //打开摄像头
                openCan();
                if (debug)
                {
                    debugFrm.Show();
                }
            }
            catch (ApplicationException err)
            {
                log.WriteLog(err.ToString());
                return;
            }

            //设置跨线程操作
            Control.CheckForIllegalCrossThreadCalls = false;
            //加载定时器
            inteverTime();
        }

        //创建定时器
        System.Timers.Timer timer = new System.Timers.Timer();
        //定时器
        private void inteverTime()
        {
            timer.Enabled = true;
            timer.Interval = 1000;//执行间隔时间,单位为毫秒
            timer.Start();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(faceRecognition);
        }

        //定时执行事件
        private void faceRecognition(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                //foreach (var item in Controls)
                //{
                //    if (item is System.Windows.Forms.PictureBox)
                //    {
                //        ((System.Windows.Forms.PictureBox)item).Image = new Bitmap(1, 1);
                //    }
                //}
                Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame();
                if (videoSourcePlayer1 == null || bitmap == null)
                {
                    throw new Exception("图像加载失败！");
                }
                //this.Refresh();
                //调取人脸识别API，数据展示
                string url = "https://api-cn.faceplusplus.com/facepp/v3/detect";
                Dictionary<string, object> verifyPostParameters = new Dictionary<string, object>();
                verifyPostParameters.Add("api_key", "YCtNBLKcbjWR7NUFJCgxMH9JekkBRqsa");
                verifyPostParameters.Add("api_secret", "L4YiS_YbR93k7eifMWQQNIkEIr-15_Fn");
                verifyPostParameters.Add("return_landmark", "0");//0：不返回人脸坐标  1：返回83对坐标  2：返回106对坐标
                verifyPostParameters.Add("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
                //verifyPostParameters.Add("calculate_all", "1");//正式api_key 才能使用此参数
                //添加图片参数

                MemoryStream ms = new MemoryStream();
                bitmap.Save(ms, ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                verifyPostParameters.Add("image_base64", Convert.ToBase64String(arr));
                string xx = createHttp(url, verifyPostParameters);
                JObject ja = (JObject)JsonConvert.DeserializeObject(xx);
                Thread t = new Thread(() => datasource(ja));
                t.Start();
            }
            catch (Exception err)
            {
                log.WriteLog(err.ToString());
                this.aq_qingxu.Value = 0;
                this.aq_qingxu_lab.Text = "";
                this.labfacescore.Text = "";
                this.labage.Text = "";
                this.labsex.Text = "";
                return;
            }

        }

        //发送人脸识别 http 请求函数
        private string createHttp(string url, Dictionary<string, object> verifyPostParameters)
        {
            HttpWebResponse verifyResponse = HttpHelper4MultipartForm.MultipartFormDataPost(url, "", verifyPostParameters);
            Stream str = verifyResponse.GetResponseStream();
            StreamReader str1 = new StreamReader(str);
            string xx = str1.ReadToEnd();
            return xx;
        }

        private void setManyidu(float value)
        {
            float v = value / 10;
            //if (this.aq_manyidu.Value + v > 100) {
            //    this.aq_manyidu.Value = 100;
            //    return;
            //}
            //else if (this.aq_manyidu.Value + v < 0)
            //{
            //    this.aq_manyidu.Value = 0;
            //    return;
            //} else
            //{
            //    this.aq_manyidu.Value += v;
            //}
        }

        

        //数据填充
        private void datasource(JObject ja)
        {
            try
            {
                if (ja.ContainsKey("error_message"))
                {
                    log.WriteLog(ja.GetValue("error_message").ToString());
                    return;
                }
                this.labtitle.Text = "";
                //设置标志为true，描出人脸框
                //flag = true;
                //this.Refresh();
                //this.aq_manyidu.Value = 0;
                float smile = Convert.ToSingle(ja["faces"][0]["attributes"]["smile"]["value"]);
                float smile_min = Convert.ToSingle(ja["faces"][0]["attributes"]["smile"]["threshold"]);
                if (debug) debugFrm.outPutDebug(string.Format("微笑程度:{0:f} 微笑阈值: {1:f} ", smile, smile_min));
                setManyidu((smile - smile_min)>0?(smile-smile_min):0); 
                //for (int my = 0; my < Convert.ToInt32(ja["faces"][0]["attributes"]["smile"]["value"]); my++)
                //{
                //    this.aq_manyidu.Value += Convert.ToByte(Convert.ToInt32(ja["faces"][0]["attributes"]["smile"]["value"]));//float 类型
                //}


                y = Convert.ToInt32(ja["faces"][0]["face_rectangle"]["top"]);
                x = Convert.ToInt32(ja["faces"][0]["face_rectangle"]["left"]);
                width = Convert.ToInt32(ja["faces"][0]["face_rectangle"]["width"]);
                height = Convert.ToInt32(ja["faces"][0]["face_rectangle"]["height"]);

                if (ja["faces"][0]["attributes"]["gender"]["value"].ToString() == "Male")
                {
                    this.labsex.Text = "小哥哥";
                    this.labfacescore.Text = Convert.ToInt32(ja["faces"][0]["attributes"]["beauty"]["male_score"]).ToString() + "分";//颜值
                    this.picturebox_sex.ImageLocation = temppath + "\\Resources\\男@3x.png";
                }
                else
                {
                    this.labsex.Text = "小姐姐";
                    this.labfacescore.Text = Convert.ToInt32(ja["faces"][0]["attributes"]["beauty"]["male_score"]).ToString() + "分";//颜值
                    this.picturebox_sex.ImageLocation = temppath + "\\Resources/女@3x.png";
                }

                //年龄
                this.labage.Text = ja["faces"][0]["attributes"]["age"]["value"].ToString() + "岁";

                //笑容度
                //if (Convert.ToInt32(ja["faces"][0]["attributes"]["smile"]["value"]) > 0 && Convert.ToInt32(ja["faces"][0]["attributes"]["smile"]["value"]) <= 40)
                //{
                //    this.aq_manyidu_lab.Text = Convert.ToInt32(ja["faces"][0]["attributes"]["smile"]["value"]).ToString();
                //}
                //else if (Convert.ToInt32(ja["faces"][0]["attributes"]["smile"]["value"]) > 40 && Convert.ToInt32(ja["faces"][0]["attributes"]["smile"]["value"]) <= 70)
                //{
                //    this.aq_manyidu_lab.Text = Convert.ToInt32(ja["faces"][0]["attributes"]["smile"]["value"]).ToString();
                //}
                //else
                //{
                //    this.aq_manyidu_lab.Text = Convert.ToInt32(ja["faces"][0]["attributes"]["smile"]["value"]).ToString();
                //}

                //情绪
                double[] emotion_arr = new double[7];
                emotion_arr.SetValue(Convert.ToSingle(ja["faces"][0]["attributes"]["emotion"]["sadness"]), 0);//sadness：伤心
                emotion_arr.SetValue(Convert.ToSingle(ja["faces"][0]["attributes"]["emotion"]["neutral"]), 1);
                emotion_arr.SetValue(Convert.ToSingle(ja["faces"][0]["attributes"]["emotion"]["disgust"]), 2);
                emotion_arr.SetValue(Convert.ToSingle(ja["faces"][0]["attributes"]["emotion"]["anger"]), 3);
                emotion_arr.SetValue(Convert.ToSingle(ja["faces"][0]["attributes"]["emotion"]["surprise"]), 4);
                emotion_arr.SetValue(Convert.ToSingle(ja["faces"][0]["attributes"]["emotion"]["fear"]), 5);
                emotion_arr.SetValue(Convert.ToSingle(ja["faces"][0]["attributes"]["emotion"]["happiness"]), 6);
                ArrayList list = new ArrayList(emotion_arr);
                list.Sort();
                float max = Convert.ToSingle(list[list.Count - 1]);
                int index = Array.IndexOf(emotion_arr, max);
                if (debug) debugFrm.outPutDebug(string.Format("伤心:{0:f} ", emotion_arr[0]));
                if (debug) debugFrm.outPutDebug(string.Format("平静:{0:f} ", emotion_arr[1]));
                if (debug) debugFrm.outPutDebug(string.Format("厌恶:{0:f} ", emotion_arr[2]));
                if (debug) debugFrm.outPutDebug(string.Format("愤怒:{0:f} ", emotion_arr[3]));
                if (debug) debugFrm.outPutDebug(string.Format("惊讶:{0:f} ", emotion_arr[4]));
                if (debug) debugFrm.outPutDebug(string.Format("恐惧:{0:f} ", emotion_arr[5]));
                if (debug) debugFrm.outPutDebug(string.Format("高兴:{0:f} ", emotion_arr[6]));
                string emotion = "";
                switch(index)
                {
                    case 0: emotion = "伤心"; setManyidu(-max); break;
                    case 1: emotion = "平静"; break;
                    case 2: emotion = "厌恶"; setManyidu(-max); break;
                    case 3: emotion = "愤怒"; setManyidu(-max); break;
                    case 4: emotion = "惊讶"; break;
                    case 5: emotion = "恐惧"; break;
                    case 6: emotion = "高兴"; setManyidu(max); break;
                }
                if (debug) debugFrm.outPutDebug(string.Format("当前表情: {0:s}\r\n", emotion));

                switch (index)
                {
                    case 0://sadness：伤心
                        if (emotion_arr[index] <= 33)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "愁眉苦脸";
                            this.lab_qx_left.Text = "垂头丧气";
                            this.lab_qx_right.Text = "悲痛欲绝";
                        }
                        else if (emotion_arr[index] > 33 && emotion_arr[index] <= 66)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "愁眉苦脸";
                            this.lab_qx_left.Text = "垂头丧气";
                            this.lab_qx_right.Text = "悲痛欲绝";
                        }
                        else
                        {
                            this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "愁眉苦脸";
                            this.lab_qx_left.Text = "垂头丧气";
                            this.lab_qx_right.Text = "悲痛欲绝";
                        }
                        break;
                    case 1://平静
                        if (emotion_arr[index] <= 33)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "波澜不惊";
                            this.lab_qx_left.Text = "心平气和";
                            this.lab_qx_right.Text = "心如止水";
                        }
                        else if (emotion_arr[index] > 33 && emotion_arr[index] <= 66)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "波澜不惊";
                            this.lab_qx_left.Text = "心平气和";
                            this.lab_qx_right.Text = "心如止水";
                        }
                        else
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "波澜不惊";
                            this.lab_qx_left.Text = "心平气和";
                            this.lab_qx_right.Text = "心如止水";
                        }
                        break;
                    case 2://disgust：厌恶
                        if (emotion_arr[index] <= 33)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "不屑一顾";
                            this.lab_qx_left.Text = "恨之入骨";
                            this.lab_qx_right.Text = "深恶痛绝";
                        }
                        else if (emotion_arr[index] > 33 && emotion_arr[index] <= 66)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString(); this.lab_qx_left_bottom.Text = "不屑一顾";
                            this.lab_qx_left.Text = "恨之入骨";
                            this.lab_qx_right.Text = "深恶痛绝";
                        }
                        else
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "不屑一顾";
                            this.lab_qx_left.Text = "恨之入骨";
                            this.lab_qx_right.Text = "深恶痛绝";
                        }
                        break;
                    case 3://anger：愤怒
                        if (emotion_arr[index] <= 33)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "怒气冲冲";
                            this.lab_qx_left.Text = "怒火中烧";
                            this.lab_qx_right.Text = "火冒三丈";
                        }
                        else if (emotion_arr[index] > 33 && emotion_arr[index] <= 66)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "怒气冲冲";
                            this.lab_qx_left.Text = "怒火中烧";
                            this.lab_qx_right.Text = "火冒三丈";
                        }
                        else
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "怒气冲冲";
                            this.lab_qx_left.Text = "怒火中烧";
                            this.lab_qx_right.Text = "火冒三丈";
                        }
                        break;
                    case 4://surprise：惊讶
                        if (emotion_arr[index] <= 33)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "目瞪口呆";
                            this.lab_qx_left.Text = "大吃一惊";
                            this.lab_qx_right.Text = "大惊失色";
                        }
                        else if (emotion_arr[index] > 33 && emotion_arr[index] <= 66)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "目瞪口呆";
                            this.lab_qx_left.Text = "大吃一惊";
                            this.lab_qx_right.Text = "大惊失色";
                        }
                        else
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "目瞪口呆";
                            this.lab_qx_left.Text = "大吃一惊";
                            this.lab_qx_right.Text = "大惊失色";
                        }
                        break;
                    case 5://fear：恐惧
                        if (emotion_arr[index] <= 33)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "惶恐不安";
                            this.lab_qx_left.Text = "胆战心惊";
                            this.lab_qx_right.Text = "惊恐万分";
                        }
                        else if (emotion_arr[index] > 33 && emotion_arr[index] <= 66)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "惶恐不安";
                            this.lab_qx_left.Text = "胆战心惊";
                            this.lab_qx_right.Text = "惊恐万分";
                        }
                        else
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "惶恐不安";
                            this.lab_qx_left.Text = "胆战心惊";
                            this.lab_qx_right.Text = "惊恐万分";
                        }
                        break;
                    case 6://happiness：高兴
                        if (emotion_arr[index] <= 33)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "眉开眼笑";
                            this.lab_qx_left.Text = "欢天喜地";
                            this.lab_qx_right.Text = "欣喜若狂";
                        }
                        else if (emotion_arr[index] > 33 && emotion_arr[index] <= 66)
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "眉开眼笑";
                            this.lab_qx_left.Text = "欢天喜地";
                            this.lab_qx_right.Text = "欣喜若狂";
                        }
                        else
                        {
                            //this.aq_qingxu.Value = Convert.ToByte(Convert.ToInt32(emotion_arr[index]) * 5);//float 类型  仪表盘
                            //this.aq_qingxu_lab.Text = Convert.ToInt32(emotion_arr[index]).ToString();
                            this.lab_qx_left_bottom.Text = "眉开眼笑";
                            this.lab_qx_left.Text = "欢天喜地";
                            this.lab_qx_right.Text = "欣喜若狂";
                        }
                        break;
                    default:
                        break;
                }
                int jk = (int)Convert.ToInt32(ja["faces"][0]["attributes"]["skinstatus"]["health"]) / 10;
                if (debug) debugFrm.outPutDebug(string.Format("健康数值: {0:s}\r\n", jk));
                for (int face1 = 0; face1 < jk; jk++)
                {
                    this.jk01.ImageLocation=temppath + "\\Resources\\555551.png";
                    string a = "jk0" + face1;
                }
                //面部特征
                //this.progressBar1.Value = Convert.ToInt32(ja["faces"][0]["attributes"]["skinstatus"]["health"]);
                //this.progressBar2.Value = Convert.ToInt32(ja["faces"][0]["attributes"]["skinstatus"]["stain"]);
                //this.progressBar3.Value = Convert.ToInt32(ja["faces"][0]["attributes"]["skinstatus"]["acne"]);
                //this.progressBar4.Value = Convert.ToInt32(ja["faces"][0]["attributes"]["skinstatus"]["dark_circle"]);
                //this.labmianbu_1.Text = (Convert.ToInt32(ja["faces"][0]["attributes"]["skinstatus"]["health"])).ToString();
                //this.labmianbu_2.Text = (Convert.ToInt32(ja["faces"][0]["attributes"]["skinstatus"]["stain"])).ToString();
                //this.labmianbu_3.Text = (Convert.ToInt32(ja["faces"][0]["attributes"]["skinstatus"]["acne"])).ToString();
                //this.labmianbu_4.Text = (Convert.ToInt32(ja["faces"][0]["attributes"]["skinstatus"]["dark_circle"])).ToString();
            }
            catch (Exception err)
            {
                log.WriteLog(err.ToString());

                return;
            }
        }

        /// <summary>
        /// 打开摄像头
        /// </summary>
        public void openCan()
        {
            //连接摄像头。
            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
            videoSource.VideoResolution = videoSource.VideoCapabilities[0];
            // 枚举所有摄像头支持的像素，设置拍照为1920*1080
            foreach (VideoCapabilities capab in videoSource.VideoCapabilities)
            {
                if (capab.FrameSize.Width == 1920 && capab.FrameSize.Height == 1080)
                {
                    videoSource.VideoResolution = capab;
                    break;
                }
                if (capab.FrameSize.Width == 1280 && capab.FrameSize.Height == 720)
                {
                    videoSource.VideoResolution = capab;
                    break;
                }
            }
            videoSourcePlayer1.NewFrame += new VideoSourcePlayer.NewFrameHandler(videoSourcePlayer1_NewFrame);
            videoSourcePlayer1.VideoSource = videoSource;

            // set NewFrame event handler
            videoSourcePlayer1.Start();
        }

        private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
        {
            Image img = System.Drawing.Image.FromFile(temppath + "\\Resources\\face.png");        //粘贴的图片
            Graphics gph = Graphics.FromImage(image);
            Pen myPen = new Pen(Color.Blue, 4);
            int left_top_x = x;
            int left_top_y = y;
            int left_bottom_x = left_top_x;
            int left_bottom_y = left_top_y + height;
            int right_top_x = left_top_x + width;
            int right_top_y = left_top_y;
            int right_bottom_x = left_bottom_x + width;
            int right_bottom_y = left_bottom_y;
            //gph.DrawEllipse(myPen, x, y, width, height);//画椭圆的方法，x坐标、y坐标、宽、高，如果是100，则半径为50
            gph.DrawImage(img, x-100, y-100, width+200, height+200);
            GC.Collect();
        }

        private void FrmMain_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)27)
            {
                Environment.Exit(0);
                return;
            }
        }

    }
}
